// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   CMIInteractions.java

package com.iphaeton.lms.course.scorm12.datamodel.cmi;

import com.iphaeton.lms.course.scorm12.DebugIndicator;
import java.io.PrintStream;
import java.io.Serializable;
import java.util.Vector;

// Referenced classes of package com.iphaeton.lms.course.scorm12.datamodel.cmi:
//            CMICategory, CMIRequest, DMErrorManager, CMIInteractionData

public class CMIInteractions extends CMICategory
    implements Serializable
{

    public CMIInteractions()
    {
        super(false);
        interactions = new Vector();
    }

    public Vector getInteractions()
    {
        return interactions;
    }

    public void setInteractions(CMIInteractionData inInteractions, int index)
    {
        interactions.add(index, inInteractions);
    }

    public String getChildren()
    {
        String children = "id,objectives,time,type,correct_responses,weighting,student_response,result,latency";
        return children;
    }

    public void performSet(CMIRequest theRequest, DMErrorManager dmErrorMgr)
    {
        if(_Debug)
            System.out.println("\tIn CMIInteractions::performSet()");
        String token = theRequest.getNextToken();
        if(_Debug)
            System.out.println("\tToken being processed: " + token);
        if(theRequest.isAKeywordRequest())
        {
            dmErrorMgr.recKeyWordError(token);
        } else
        {
            int index = -1;
            try
            {
                Integer tmpInt = new Integer(token);
                index = tmpInt.intValue();
                CMIInteractionData tmpInteraction = (CMIInteractionData)interactions.elementAt(index);
                tmpInteraction.performSet(theRequest, dmErrorMgr);
                interactions.set(index, tmpInteraction);
            }
            catch(NumberFormatException nfe)
            {
                if(_Debug)
                {
                    System.out.println("\tError - Data Model Element not implemented");
                    System.out.println("\tInvalid data model element: " + theRequest.getRequest() + " passed to LMSSetValue()");
                }
                dmErrorMgr.recNotImplementedError(theRequest);
            }
            catch(ArrayIndexOutOfBoundsException e)
            {
                if(_Debug)
                    System.out.println("\tFirst time setting the interaction data at the given index");
                if(index <= interactions.size())
                {
                    CMIInteractionData intData = new CMIInteractionData();
                    intData.performSet(theRequest, dmErrorMgr);
                    interactions.add(index, intData);
                } else
                {
                    dmErrorMgr.SetCurrentErrorCode("201");
                }
            }
        }
        theRequest.done();
        if(_Debug)
            System.out.println("\tCMIInteractions - performSet() exit");
    }

    public String performGet(CMIRequest theRequest, DMErrorManager dmErrorMgr)
    {
        String result = new String("");
        int tokenCount = theRequest.getTotalNumTokens();
        if(tokenCount == 3 && theRequest.isAChildrenRequest())
            result = getChildren();
        else
        if(theRequest.isACountRequest())
        {
            if(tokenCount == 3)
            {
                int count = howManyInteractions();
                Integer tmpInt = new Integer(count);
                result = tmpInt.toString();
            } else
            {
                String token = theRequest.getNextToken();
                try
                {
                    Integer tmpInt = new Integer(token);
                    int index = tmpInt.intValue();
                    CMIInteractionData intData = (CMIInteractionData)interactions.elementAt(index);
                    if(intData.isInitialized())
                    {
                        result = intData.getCount(theRequest, dmErrorMgr);
                    } else
                    {
                        String nextToken = theRequest.getNextToken();
                        if(nextToken.equals("objectives") || nextToken.equals("correct_responses"))
                            result = "0";
                        else
                            dmErrorMgr.recGetKeyWordError(theRequest.getElement());
                    }
                }
                catch(NumberFormatException nfe)
                {
                    dmErrorMgr.recNotImplementedError(theRequest);
                }
                catch(ArrayIndexOutOfBoundsException e)
                {
                    String nextToken = theRequest.getNextToken();
                    if(tokenCount == 5 && (nextToken.equals("objectives") || nextToken.equals("correct_responses")))
                    {
                        result = "0";
                    } else
                    {
                        dmErrorMgr.recGetKeyWordError(theRequest.getElement());
                        result = "";
                    }
                }
            }
        } else
        {
            if(_Debug)
            {
                System.out.println("\tInvalid LMSGetValue() request: " + theRequest.getRequest());
                System.out.println("\tSCO is not permitted to call LMSGetValue() for cmi.interactions");
            }
            dmErrorMgr.SetCurrentErrorCode("404");
        }
        if(_Debug)
        {
            System.out.println("\tReturning: " + result + " from CMIInteractions");
            System.out.println("\tCMIInteractions::performGet() exit");
        }
        theRequest.done();
        return result;
    }

    private int howManyInteractions()
    {
        int count = 0;
        count = interactions.size();
        return count;
    }

    private static final long serialVersionUID = 0x4eed7e3846a2557eL;
    private static boolean _Debug;
    public Vector interactions;

    static 
    {
        _Debug = DebugIndicator.ON;
    }
}
